/*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <sourcemod>
#include <sdktools>
#include <l4d_stocks>
#include <l4d_weapon_stocks>

#include "macros.sp"
#include "helpers.sp"

new Handle:cvar_distance = INVALID_HANDLE;
new bool:can_grab[MAXCLIENTS] = { true, ...};

public OnPluginStart()
{
	cvar_distance = CreateConVar("l4d2_incapgrab_distance", "70", "Distance between player and item to grab it");

	//Execute the config file
	AutoExecConfig(true, "l4d2_incapgrab");

	HookEvent("round_start", Event_RoundStart);

}

public Event_RoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
	for(new i=0; i < MAXCLIENTS; i++)
	{
		can_grab[i] = true;
	}
}

public Action:can_grab_again(Handle:timer, any:client)
{
	can_grab[client] = true;
}

public Action:OnPlayerRunCmd(client, &buttons, &impulse, Float:vel[3], Float:angles[3], &weapon)
{
	if(!IsValidHumanSurvivorAlive(client))
	{
		return;
	}

	if(!L4D_IsPlayerIncapacitated(client))
	{
		return;
	}

	if (buttons & IN_DUCK)
	{
		new target = GetClientAimTarget(client, false);
		if(!IsValidEntity(target) || !IsValidEdict(target))
		{
			return;
		}

		if(!can_grab[client])
		{
			return;
		}

		/* verify player is not too far */
		/*get the 2 position */
		decl Float:ent_pos[3];
		decl Float:player_pos[3];
		GetClientAbsOrigin(client, player_pos);
		GetEntityAbsOrigin(target, ent_pos);
		
		/*make a vector */
		decl Float:dist_vec[3];
		MakeVectorFromPoints(ent_pos, player_pos, dist_vec);
		new Float:dist = GetVectorLength(dist_vec);
		
		/* check distance */
		if(dist > GetConVarFloat(cvar_distance))
		{
			return;
		}

		/* distance is ok, check the item */
		decl String:ent_name[32];
		if (GetEntityClassname(target, ent_name, sizeof(ent_name)))
		{
	
			if(strcmp(ent_name, "weapon_pain_pills") == 0 || strcmp(ent_name, "weapon_pain_pills_spawn") == 0)
			{
				can_grab[client] = false;
				CreateTimer(1.0, can_grab_again, client, TIMER_FLAG_NO_MAPCHANGE);
				AcceptEntityInput(target, "Kill");
				CheatCommand(client, "give", "pain_pills");
				PrintToChatAllExclude(client, -1, "\x01Incapped \x04%N\x01 grab \x04pills\x01 with \x04DUCK\x01 button.", client);
				PrintToChat(client, "\x01You grab \x05pills\x01.");
			}
			else if(strcmp(ent_name, "weapon_adrenaline") == 0 || strcmp(ent_name, "weapon_adrenaline_spawn") == 0)
			{
				can_grab[client] = false;
				CreateTimer(1.0, can_grab_again, client, TIMER_FLAG_NO_MAPCHANGE);
				AcceptEntityInput(target, "Kill");
				CheatCommand(client, "give", "adrenaline");
				PrintToChatAllExclude(client, -1, "\x01Incapped \x04%N\x01 grab \x04adrenaline\x01 with \x04DUCK\x01 button.", client);
				PrintToChat(client, "\x01You grab \x05adrenaline\x01.");
			}
			else if(strcmp(ent_name, "weapon_molotov") == 0 || strcmp(ent_name, "weapon_molotov_spawn") == 0)
			{
				can_grab[client] = false;
				CreateTimer(1.0, can_grab_again, client, TIMER_FLAG_NO_MAPCHANGE);
				AcceptEntityInput(target, "Kill");
				CheatCommand(client, "give", "molotov");
				PrintToChatAllExclude(client, -1, "\x01Incapped \x04%N\x01 grab \x04molotov\x01 with \x04DUCK\x01 button.", client);
				PrintToChat(client, "\x01You grab \x05molotov\x01.");
			}
			else if(strcmp(ent_name, "weapon_pipe_bomb") == 0 || strcmp(ent_name, "weapon_pipe_bomb_spawn") == 0)
			{
				can_grab[client] = false;
				CreateTimer(1.0, can_grab_again, client, TIMER_FLAG_NO_MAPCHANGE);
				AcceptEntityInput(target, "Kill");
				CheatCommand(client, "give", "pipe_bomb");
				PrintToChatAllExclude(client, -1, "\x01Incapped \x04%N\x01 grab \x04pipe bomb\x01 with \x04DUCK\x01 button.", client);
				PrintToChat(client, "\x01You grab \x05pipe bomb\x01.");
			}
			else if(strcmp(ent_name, "weapon_vomitjar") == 0 || strcmp(ent_name, "weapon_vomitjar_spawn") == 0)
			{
				can_grab[client] = false;
				CreateTimer(1.0, can_grab_again, client, TIMER_FLAG_NO_MAPCHANGE);
				AcceptEntityInput(target, "Kill");
				CheatCommand(client, "give", "vomitjar");
				PrintToChatAllExclude(client, -1, "\x01Incapped \x04%N\x01 grab \x04vomitjar\x01 with \x04DUCK\x01 button.", client);
				PrintToChat(client, "\x01You grab \x05vomit jar\x01.");
			}
			else if(strcmp(ent_name, "weapon_first_aid_kit") == 0 || strcmp(ent_name, "weapon_first_aid_kit_spawn") == 0)
			{
				can_grab[client] = false;
				CreateTimer(1.0, can_grab_again, client, TIMER_FLAG_NO_MAPCHANGE);
				AcceptEntityInput(target, "Kill");
				CheatCommand(client, "give", "first_aid_kit");
				PrintToChatAllExclude(client, -1, "\x01Incapped \x04%N\x01 grab \x04first aid kit\x01 with \x04DUCK\x01 button.", client);
				PrintToChat(client, "\x01You grab \x05first aid kit\x01.");
			}
			else if(strcmp(ent_name, "weapon_defibrillator") == 0 || strcmp(ent_name, "weapon_defibrillator_spawn") == 0)
			{
				can_grab[client] = false;
				CreateTimer(1.0, can_grab_again, client, TIMER_FLAG_NO_MAPCHANGE);
				AcceptEntityInput(target, "Kill");
				CheatCommand(client, "give", "defibrillator");
				PrintToChatAllExclude(client, -1, "\x01Incapped \x04%N\x01 grab \x04defibrillator\x01 with \x04DUCK\x01 button.", client);
				PrintToChat(client, "\x01You grab \x05defibrillator\x01.");
			}


		}
		else
		{
			return;
		}

	}
}
